﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using TextMining.Helper;
using Microsoft.Office.Interop.Word;
using System.Web.Services;
using TextMining.Models;
using TextMining.Service;

namespace TextMining
{
    public partial class Classify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            panelRes.Visible = false;
        }

       
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string sContent = txtContent.Text.Trim();
            MyDoc myDoc = new MyDoc();
            myDoc.Source = new Dictionary<string, int>();

            if (!string.IsNullOrEmpty(sContent))
            {
                myDoc.ShortContent = sContent;
                myDoc.FullContent = sContent;
                myDoc.ShortContent = sContent;
                myDoc.FullContent = sContent;

                if (myDoc.FullContent.Length > 0)
                {
                    FileService fileSr = new FileService();

                    fileSr.InitialSource(myDoc);

                    fileSr.GetTrainingSet();

                    fileSr.CalculateVector();

                    fileSr.CalPC();
                    string sResultCate = fileSr.CalPC_over_Doc();
                    myDoc.Category = sResultCate.Split('|')[0];
                    int iIndexCate = Convert.ToInt32(sResultCate.Split('|')[1]);
                    myDoc.ErrorStr = string.Empty;
                    myDoc.Tag = new List<string>();

                    //Add tag

                    var dicTags = fileSr.ArrCate[iIndexCate] as Dictionary<string, int>;

                    string[] arrWordCate = fileSr.ArrWordCate[iIndexCate] as string[];


                    var sortedDict = (from entry in dicTags as Dictionary<string, int> orderby entry.Value descending select entry)
                        .Where(p => CheckExistWord(arrWordCate, p.Key.ToString()) == true)
                        .Take(5)
                        .ToDictionary(pair => pair.Key, pair => pair.Value)
                    ;

                    if (sortedDict.Count > 0)
                    {
                        foreach (KeyValuePair<string, int> item in sortedDict)
                        {
                            myDoc.Tag.Add(item.Key.ToString());
                        }
                    }
                    //End Add tag

                }
                else
                    myDoc.ErrorStr = "Xin vui long nhap du lieu phan tich";

            }
            else
            {
                spResult.Text = "Xin vui lòng upload file hoặc nhập trực tiếp nội dung vào ô nhập liệu";
                myDoc.Tag = new List<string>();
                myDoc.Category = string.Empty;
                myDoc.ErrorStr = string.Empty;
            }

            panelRes.Visible = true;
            if (myDoc.Tag.Count > 0)
            {
                lbCateResult.Text = myDoc.Category;
                panSucc.Visible = true;
                panError.Visible = false;
            }
            else
            {
                lbCateResult.Text = string.Empty;
                panSucc.Visible = false;
                panError.Visible = true;
            }
            lbExist.Text = string.Empty;
            
            foreach (var item in myDoc.Tag)
            {
                int iCount = CodeHepler.CountWordInDoc(item, myDoc.FullContent);
                if (iCount > 0)
                    lbExist.Text += item + "(" + iCount.ToString() + "), ";
                else
                    lbExist.Text += item + ", ";
            }

            
            //lbExist = lbExist.

        }

        

        public bool CheckExistWord(string[] arrWord, string sWord)
        {
            bool bResult = false;
            foreach (var item in arrWord)
            {
                if (string.Equals(sWord, item))
                    bResult = true;
            }
            return bResult;
        }

    }
}